package com.driver.core.convert;

import com.driver.core.Column;
import com.driver.core.DBManager;
import com.driver.core.StringUtil;
import com.driver.core.Table;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SqlServerService extends AbstractSqlService {
    public static Set<String> set = new HashSet<>();

    private SqlServerService() {
    }

    public static SqlServerService getInstance() {
        return SqlServerConvertSingle.INSTANCE;

    }

    private static class SqlServerConvertSingle {
        private static final SqlServerService INSTANCE = new SqlServerService();
    }


    @Override
    public String columnTypeToJavaType(String columnType) {
        set.add(columnType);
        if (columnType.equalsIgnoreCase("bigint")) {
            return "Long";
        } else if (columnType.equalsIgnoreCase("bigint identity")) {
            return "Long";
        } else if (columnType.equalsIgnoreCase("int")) {
            return "Integer";
        } else if (columnType.equalsIgnoreCase("int identity")) {
            return "Integer";
        } else if (columnType.contains("char") || columnType.equalsIgnoreCase("xml") || columnType.equalsIgnoreCase("text")) {
            return "String";
        } else if (columnType.equalsIgnoreCase("datetime") || columnType.equalsIgnoreCase("smalldatetime") || columnType.equalsIgnoreCase("date") || columnType.equalsIgnoreCase("datetime2")) {
            return "Date";
        } else if (columnType.equalsIgnoreCase("decimal") || columnType.equalsIgnoreCase("numeric") || columnType.equalsIgnoreCase("money")) {
            return "BigDecimal";
        } else if (columnType.equalsIgnoreCase("smallint")) {
            return "Short";
        } else if (columnType.equalsIgnoreCase("tinyint")) {
            return "Byte";
        } else if (columnType.equalsIgnoreCase("bit")) {
            return "Boolean";
        } else if (columnType.equalsIgnoreCase("float")) {
            return "Float";
        } else if (columnType.equalsIgnoreCase("double")) {
            return "Double";
        }
        throw new RuntimeException("未知的字段类型" + columnType + "无法转换");
    }

    @Override
    public List<Table> getTable() throws SQLException {
        return super.getTable(new ICallBack() {
            @Override
            public void getPrivateKey(DatabaseMetaData dbMetaData, List<Column> columns) throws SQLException {
                ResultSet pkInfo = dbMetaData.getPrimaryKeys(null, null, DBManager.getConfig().getTable());
                while (pkInfo.next()) {
                    String privateKey = (String) pkInfo.getObject(4);
                    for (Column column : columns) {
                        if (column.getColumnName().equals(privateKey)) {
                            column.setPrivateKey(true);
                        }
                    }
                }
            }
        });
    }
}
